package ngames.com.sushitv.action;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import ngames.com.sushitv.R;
import ngames.com.sushitv.activity.LoginActivity;
import ngames.com.sushitv.base.BaseAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.interfaces.DialogButtonListener;
import ngames.com.sushitv.tools.AppUtil;
import ngames.com.sushitv.view.DialogApp;

/**
 * Created by Jeffrey on 2016/8/12.
 */
public class ResetPasswordAction extends BaseAction implements View.OnClickListener {
    private EditText mPasswordEdt;
    private EditText mConfirmPasswordEdt;

    public ResetPasswordAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        View baseView = mBaseView;
        setTitle(R.string.reset_password);
        mPasswordEdt = (EditText) baseView.findViewById(R.id.edt_password);
        mConfirmPasswordEdt = (EditText) baseView.findViewById(R.id.edt_confirm_password);
        baseView.findViewById(R.id.btn_reset).setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_reset:
                attemptResetPassword();
                break;
        }
    }

    private void attemptResetPassword() {
        mPasswordEdt.setError(null);
        mConfirmPasswordEdt.setError(null);

        String password = mPasswordEdt.getText().toString();
        String confirmPassword = mConfirmPasswordEdt.getText().toString();

        boolean cancel = false;
        View focusView = null;

        if (TextUtils.isEmpty(password)) {
            mPasswordEdt.setError(getString(R.string.enter_password));
            focusView = mPasswordEdt;
            cancel = true;
        } else if (!AppUtil.isPasswordValid(password)) {
            mPasswordEdt.setError(getString(R.string.error_invalid_password));
            focusView = mPasswordEdt;
            cancel = true;
        } else if (TextUtils.isEmpty(confirmPassword)) {
            mConfirmPasswordEdt.setError(getString(R.string.error_field_required));
            focusView = mConfirmPasswordEdt;
            cancel = true;
        } else if (!password.equals(confirmPassword)) {
            mConfirmPasswordEdt.setError(getString(R.string.error_invalid_password));
            focusView = mConfirmPasswordEdt;
            cancel = true;
        }
        if (cancel) {
            focusView.requestFocus();
        } else {
            showProgress(true);
            resetPassword(password);
        }
    }

    private void resetPassword(String password) {
        getAppModel().getUserService().resetPassword(password, new ModelCallback<Void>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(Void data) {
                showProgress(false);
                final DialogApp activationDialog = new DialogApp(mActivity);
                activationDialog.setMessage("You have reset");
                activationDialog.setRightBtn("OK", new DialogButtonListener() {
                    @Override
                    public void onClick() {
                        activationDialog.dismiss();
                        mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                    }
                });
                activationDialog.show();
            }

            @Override
            public void onFailure(String errorEvent, String message) {
                showProgress(false);
                Toast.makeText(getAppApplication(), "error", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
